package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.ElevatorEvent;
import com.gc.business.service.IElevatorEventService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯事件 控制层
 * 
 * @author hruiyan
 * @date 2020-11-16
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorEvent")
public class ElevatorEventController extends BaseController {
    private String prefix = "business/elevatorEvent";

	@Autowired
	private ISysUserService userService;
	
	private final IElevatorEventService elevatorEventService;

	@Autowired
	public ElevatorEventController(IElevatorEventService elevatorEventService) {
		this.elevatorEventService = elevatorEventService;
	}

	/**
	*  历史事件列表页
	*/
	@RequiresPermissions("business:elevatorEvent:view")
	@GetMapping("/historyEvent")
	public String historyEvent(){
	    return prefix + "/historyEvent";
	}

	/**
	 *  列表页
	 */
	@RequiresPermissions("business:elevatorEvent:view")
	@GetMapping
	public String elevatorEvent(){
		return prefix + "/elevatorEvent";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{eventId}")
	public String edit(@PathVariable("eventId") Long eventId, ModelMap mmap){
		ElevatorEvent elevatorEvent = elevatorEventService.getById(eventId);
		mmap.put("elevatorEvent", elevatorEvent);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯事件列表
	 */
	@RequiresPermissions("business:elevatorEvent:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(ElevatorEvent elevatorEvent){

		Long attrId = getAttrId();
		if(userService.ifCompanyRole(attrId)){
			elevatorEvent.setCompanyId(attrId);
			startPage();
			List<ElevatorEvent> list = elevatorEventService.selectElevatorEventList(elevatorEvent);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<ElevatorEvent>()));
		}
	}

	/**
	 * 新增保存电梯事件
	 */
	@RequiresPermissions("business:elevatorEvent:add")
	@Log(title = "电梯事件", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ElevatorEvent elevatorEvent){
		elevatorEventService.save(elevatorEvent);
		return Result.success();
	}

	/**
	 * 修改保存电梯事件
	 */
	@RequiresPermissions("business:elevatorEvent:edit")
	@Log(title = "电梯事件", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ElevatorEvent elevatorEvent){
		elevatorEventService.updateById(elevatorEvent);
		return Result.success();
	}
	
	/**
	 * 删除电梯事件
	 */
	@RequiresPermissions("business:elevatorEvent:remove")
	@Log(title = "电梯事件", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		elevatorEventService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯事件列表
	 */
	@RequiresPermissions("business:elevatorEvent:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ElevatorEvent elevatorEvent){
		List<ElevatorEvent> list = elevatorEventService.list(elevatorEvent);
		ExcelUtil<ElevatorEvent> util = new ExcelUtil<>(ElevatorEvent.class);
		util.exportExcel(list, "elevatorEvent");
		return Result.success();
	}
}
